package com.thinkgem.jeesite.common.utils.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang3.StringUtils;

import com.thinkgem.jeesite.common.config.Global;



public class EmailUtils {
	
	private String host;  //邮件服务器
	private String port;  //邮件发送端口
	private String usernames;  //发件邮箱账户
	private String passwords;  //发件邮箱密码
	private String mail_from;  //发件邮箱地址
	private String personalName;  //发件人显示名称
	
	private transient Session session;
	
	public EmailUtils(String host, String from, String username, String password, String personalName){
		this.host = host;
		this.port = "25";
		this.mail_from = from;
		this.usernames = username;
		this.passwords = password;
		this.personalName = personalName;
	}
	
	public EmailUtils(){
		this.host = Global.getConfig("mailServerHost");
		this.port = Global.getConfig("mailServerPort");
		this.mail_from = Global.getConfig("mailServerDefaultFrom");
		this.usernames = Global.getConfig("mailServerUsername");
		this.passwords = Global.getConfig("mailServerPassword");
		this.personalName = Global.getConfig("mailServerPersonalName");
	}

	/**
	 * 
	 * @param subject  主题
	 * @param sendmessage  内容
	 * @param toemail  收件人，多个地址直接“,”隔开
	 * @param cc  抄送，多个地址直接“,”隔开 ，可为空
	 * @return
	 */
	public boolean send(String subject, String sendMessage, String toemail, String cc) {

		String mail_body = sendMessage;
		try {
			Properties props = new Properties(); // 获取系统环境
			Authenticator auth = new Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {// 进行邮件服务器用户认证
					return new PasswordAuthentication(usernames, passwords);
				}
			};
			//props.put("mail.smtp.starttls.enable", "true");
			props.setProperty("mail.smtp.ssl.trust", "smtpserver");
			props.put("mail.smtp.host", host);
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.port", port);
			session = Session.getInstance(props, auth);// 设置session,和邮件服务器进行通讯。
			MimeMessage message = new MimeMessage(session);
			message.setSubject(subject); // 设置邮件主题
			message.setContent(mail_body, "text/html;charset=utf-8"); // 设置邮件格式
			message.setSentDate(new Date()); // 设置邮件发送日期
			Address address = new InternetAddress(mail_from, personalName);
			message.setFrom(address); // 设置邮件发送者的地址
			Address[] toAddress = new InternetAddress().parse(toemail); // 设置邮件接收方的地址
			message.addRecipients(Message.RecipientType.TO, toAddress);
			if(!StringUtils.isBlank(cc)){
				Address[] ccAddress = new InternetAddress().parse(cc); // 设置邮件抄送的地址
				message.addRecipients(Message.RecipientType.CC, ccAddress);
			}
			
			//保存邮件
		    message.saveChanges();
			
			Transport.send(message); // 发送邮件
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
	}

}
